<?php
	class SecretaryMain_Model extends Model {
		public function __construct() {
	        parent::__construct();
	    }
	    
		function checkSchedule() {
			$idEvent = $_POST["eventId"];
			$resultSchedule = $this->db->select("SELECT * FROM doctor_schedules where (id = $idEvent)");
			
			$id_patient = $_POST["userId"];
			$id_socialhealth = $_POST["healthId"];
			$id_speciality = $resultSchedule[0]['id_speciality'];
			$id_doctor = $resultSchedule[0]['id_doctor'];
			$date = $resultSchedule[0]['day'];
			$time = $resultSchedule[0]['start'];
			
			$this->db->insert('schedule', array('id_patient' => $id_patient, 'id_doctor' => $id_doctor,
				'id_socialhealth' => $id_socialhealth, 'id_speciality' => $id_speciality, 'date' => $date,
				'time' => $time));
		}
		
		function getAllEnableDoctors() {
			$result = $this -> db -> select("SELECT * FROM doctor");
			foreach ($result as $key => $value) {
				$id = $value["id"];
				$speciality = $this -> db -> select("SELECT s.id, s.text as speciality from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
				$socialhealth = $this -> db -> select("SELECT s.id, s.text as socialhealth from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
				$license = $this->db->select("SELECT * FROM license l where (l.until > CURDATE() and l.id_doctor = $id)");
				$result[$key]['has_license'] = (sizeof($license)>0)?'1':'0';
				$result[$key]['speciality'] = $speciality;
				$result[$key]['socialhealth'] = $socialhealth;
			}
			$true_res = array();
			for ($i=0; $i < sizeof($result) ; $i++) { 
				if ($result[$i]['has_license'] == '0') {
					array_push($true_res,$result[$i]);
				}
			}
			$dt = array("aaData" => $true_res);
			echo json_encode($dt);
		}
		
		function markAsUnconcreted() {
			$id = $_POST["id"];
			$this->db->update("schedule", array("status" => "1"), "id = $id");
		}
		
		function markAsConcreted() {
			$id = $_POST["id"];
			$this->db->update("schedule", array("status" => "2"), "id = $id");
		}
		
		function alreadyExistPatient() {
			$dni = $_GET['dni'];
			$dni_type = $_GET['dni_type'];
			$result2 = $this -> db -> select("SELECT * FROM patient where (erased = 0 and dni = $dni and dni_type = $dni_type)");
			echo json_encode($result2);
		}
		
		function markAsPendant() {
			$id = $_POST["id"];
			$this->db->update("schedule", array("status" => "0"), "id = $id");
		}
		
		function markAsCancel() {
			$id = $_POST["id"];
			$this->db->update("schedule", array("status" => "3"), "id = $id");
		}
		
		function getAllScheduleForPatientCancelDt() {
			$id = $_GET["id"];
			$result = $this->db->select("SELECT s.id as 'id', s.date as 'date', s.time as 'time', d.id as 'id_doctor', sh.text as 'socialhealth', sp.text as 'speciality' FROM schedule s, doctor d, socialhealth sh, speciality sp where ((id_patient = $id) and (s.id_doctor = d.id) and (s.id_socialhealth = sh.id) and (s.id_speciality = sp.id) and (s.status = 3))");
			foreach ($result as $key => $value) {
				$id_doc = $result[$key]['id_doctor'];
				$resultDoc = $this->db->select("SELECT * from doctor where (id = $id_doc)");
				$result[$key]['doctor'] = $resultDoc;
			}
			$dt = array("aaData" => $result);
			echo json_encode($dt);
		}
		
		function getAllScheduleForPatientUnconcretedDt() {
			$id = $_GET["id"];
			$result = $this->db->select("SELECT s.id as 'id', s.date as 'date', s.time as 'time', d.id as 'id_doctor', sh.text as 'socialhealth', sp.text as 'speciality' FROM schedule s, doctor d, socialhealth sh, speciality sp where ((id_patient = $id) and (s.id_doctor = d.id) and (s.id_socialhealth = sh.id) and (s.id_speciality = sp.id) and (s.status = 1))");
			foreach ($result as $key => $value) {
				$id_doc = $result[$key]['id_doctor'];
				$resultDoc = $this->db->select("SELECT * from doctor where (id = $id_doc)");
				$result[$key]['doctor'] = $resultDoc;
			}
			$dt = array("aaData" => $result);
			echo json_encode($dt);
		}
		
		function getAllScheduleForPatientConcretedDt() {
			$id = $_GET["id"];
			$result = $this->db->select("SELECT s.id as 'id', s.date as 'date', s.time as 'time', d.id as 'id_doctor', sh.text as 'socialhealth', sp.text as 'speciality' FROM schedule s, doctor d, socialhealth sh, speciality sp where ((id_patient = $id) and (s.id_doctor = d.id) and (s.id_socialhealth = sh.id) and (s.id_speciality = sp.id) and (s.status = 2))");
			foreach ($result as $key => $value) {
				$id_doc = $result[$key]['id_doctor'];
				$resultDoc = $this->db->select("SELECT * from doctor where (id = $id_doc)");
				$result[$key]['doctor'] = $resultDoc;
			}
			$dt = array("aaData" => $result);
			echo json_encode($dt);
		}
		
		function getAllScheduleForPatientDt() {
			$id = $_GET["id"];
			$result = $this->db->select("SELECT s.id as 'id', s.date as 'date', s.time as 'time', d.id as 'id_doctor', sh.text as 'socialhealth', sp.text as 'speciality' FROM schedule s, doctor d, socialhealth sh, speciality sp where ((id_patient = $id) and (s.id_doctor = d.id) and (s.id_socialhealth = sh.id) and (s.id_speciality = sp.id) and (s.status = 0))");
			foreach ($result as $key => $value) {
				$id_doc = $result[$key]['id_doctor'];
				$resultDoc = $this->db->select("SELECT * from doctor where (id = $id_doc)");
				$result[$key]['doctor'] = $resultDoc;
			}
			$dt = array("aaData" => $result);
			echo json_encode($dt);
		}
		
		function getPatientById() {
			$id = $_GET["id"];
			$result = $this->db->select("select * from patient where (id = $id)");
			echo json_encode($result); 
		}
		
		public function xhrUpdatePatient() 
	    {
			$picture = $_POST['picture_blob'];
	        $name = $_POST['name'];
			$address = $_POST['address'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];
			$tel = $_POST['tel'];
			$dni = $_POST['dni'];
			$id = $_POST['id'];
			$dni_type = $_POST['dni_type'];
			$this->db->update('patient', array('name' => $name, 'address' => $address,  'lastname' => $lastname,
	        	'email' => $email, 'tel' => $tel, 'dni' => $dni, 'picture' => $picture, 'dni_type' => $dni_type),
	        	"id = $id");
			return true;
	    }
		
	    public function xhrInsertPatient() 
	    {
			$picture = $_POST['picture_blob'];
	        $name = $_POST['name'];
			$address = $_POST['address'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];
			$tel = $_POST['tel'];
			$dni = $_POST['dni'];
			$dni_type = $_POST['dni_type'];
	        $this->db->insert('patient', 
	        	array('name' => $name, 'address' => $address,  'lastname' => $lastname,
	        	'email' => $email, 'tel' => $tel, 'dni' => $dni, 'picture' => $picture, 'dni_type' => $dni_type));
			return true;
	    }
	    
		public function xhrGetListOfErasedPatientsDt()
	    {
	        $result = $this->db->select("SELECT * FROM patient where (erased = 1)");
			$dt = array("aaData" => $result);
			echo json_encode($dt);
	    }
		
		public function xhrGetListOfErasedPatients()
	    {
	        $result = $this->db->select("SELECT * FROM patient where (erased = 1)");
			echo json_encode($result);
	    }
		
	    public function xhrGetListOfPatients()
	    {
	        $result = $this->db->select("SELECT * FROM patient where (erased = 0)");
	        echo json_encode($result);
	    }
	    
	    public function xhrDeleteListing()
	    {
	        $id = (int) $_POST['id'];
	        $this->db->delete('data', "id = '$id'");
	    }
	}
